package top.infopub.mgr.mkt.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.mkt.ProjectSellDto;
import top.infopub.enums.mkt.ProProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.enums.mkt.ProjectFormatEnum;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.dao.AfterSummaryCoopDao;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.dao.ProjectSellDtoDao;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.mgr.mkt.service.ProjectSellDtoService;
import top.infopub.model.hr.StaffInfo;
import top.infopub.model.mkt.*;
import top.infopub.model.sg.CoopInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Warking
 * @Date 2021/4/30 15:35
 * @Version 1.0
 */
@Service
public class ProjectSellDtoServiceImpl extends GenericFlowController implements ProjectSellDtoService {

    @Autowired
    private ProjectSellDtoDao projectSellDtoDao;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;

    @Autowired
    private CollectProjectServiceImpl collectProjectService;

    @Autowired
    private ProProcessManageService proProcessManageService;

    @Autowired
    private AfterSummaryCoopDao afterSummaryCoopDao;

    //营销策划详情
    @Override
    public ProjectSellDto queryDetail(String code) throws ParseException {
        ProjectSellDto projectSellDto = new ProjectSellDto();

        ProjectInfo projectInfo = projectSellDtoDao.findByProCode(code);//id是code
        //基本信息
        //项目业态-文字映射
        ProjectFormatEnum pfe = ProjectFormatEnum.getByCode(projectInfo.getProFormat());
        if (pfe != null) {
            projectInfo.setProFormat(pfe.getName());
        }
        //营销策划表
        ProSell proSell = sellDetail(code);
        //当营销策划业主单位为空时 取上一阶段
        if (StringUtils.isBlank(proSell.getOwnerOrgSguid())){
            List<AfterSummaryCoop> summaryCoops = afterSummaryCoopDao.querySummaryCoopList(proSell.getProCode(), 0);
            if (summaryCoops.size()>0){
                for (AfterSummaryCoop summaryCoop : summaryCoops) {
                    if ("-1".equals(summaryCoop.getCoopEffect())){ //-1为业主单位的标识
                        proSell.setOwnerOrgSguid(summaryCoop.getSgCoopId());
                        break;
                    }
                }
            }
        }
        ProProcessDetailTypeEnum typeEnum = proProcessManageService.queryProProcessType(code);
        proSell.setProStage(typeEnum.getTypeName());
        projectSellDto.setSumMarketingMoney(proSell.getSumMarketingMoney());
        projectSellDto.setProSell(proSell);
        projectSellDto.setProjectInfo(projectInfo);
        //营销策划费用表
        projectSellDto.setMarketingList(proSellCostByProCode(code));

        //附件查询
        //附件信息表
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(code, ProjectFileTypeEnum.FILE_TYPE9.getCode(), null);
        projectSellDto.setFileAtt(collectProjectService.fileAttaListToArray(projectAttaList));

        return projectSellDto;
    }

    @Override
    public ArrayList<CoopInfo> selectCoopInfoIdAndCoopNameAll() {
        return projectSellDtoDao.selectCoopInfoIdAndCoopNameAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(ProjectSellDto projectSellDto, String code) {
        //营销策划
        ProSell proSell = projectSellDto.getProSell();
        //营销费用数据
        List<ProSellCost> marketingList = projectSellDto.getMarketingList();
        //拿到营销策划的ID
        String proSellId = proSell.getId();
        if (!StringUtils.isEmpty(proSellId)) {
            //先删除数据库对应的可能5条的数据
            projectSellDtoDao.deleteByProSellCostProSellId(proSellId);
        }
        //获取用户
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        //把前端数据存到表里面-保存营销费用到数据库
        for (ProSellCost proSellCost : marketingList) {
            proSellCost.setAddBy(vo.getUserName());
            proSellCost.setProCode(code);
            projectSellDtoDao.insertByProSellCostId(proSellCost);

        }
        //把前端提交的营销策划数据update到数据库
        proSell.setProCode(code);
        int t = projectSellDtoDao.updateByProSellCode(proSell);
        //前端附件信息
        //更新附件表-MKT_PROJECT_ATTACHMENT
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if (projectSellDto.getFileAtt() != null) {
            String[] fileAtt = projectSellDto.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(vo, code, file);
                attachmentList.add(attachment);
            }
        }
        //更新附件信息
        projectAttachmentDao.deleteByWhere(code, ProjectFileTypeEnum.FILE_TYPE9.getCode(), null);
        if (attachmentList.size() > 0 && attachmentList != null) {
            projectAttachmentDao.insertBatch(attachmentList);
        }
        return 1;
    }

    @Override
    public List<StaffInfo> queryPersonnel() {
        List<StaffInfo> list = projectSellDtoDao.queryPersonnel();
        return list;
    }

    @Override
    public ProSell selectByProCode(String code) {
        return projectSellDtoDao.selectByProCode(code);

    }


    //营销策划费用表查询方法
    private List<ProSellCost> proSellCostByProCode(String code) throws ParseException {
        List<ProSellCost> proSellCostList = projectSellDtoDao.proSellCostByProCode(code);
        return proSellCostList;
    }

    //查询营销策划信息方法
    private ProSell sellDetail(String code) {
        ProSell proSell = projectSellDtoDao.sellByProCode(code);
        return proSell;
    }


}
